package com.jichangxiu.generator.entity.vo;

import cn.hutool.core.util.ArrayUtil;
import com.jichangxiu.generator.constant.GeneratorConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * GenColumnVo:
 *
 * @author JiChangXiu
 * @create 2024-06-30 13:31:02
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema(description = "生成列表")
public class GenColumnVo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @Schema(description = "ID")
    private String id;

    /**
     * 行ID
     */
    @Schema(description = "表ID")
    private String rowId;

    /**
     * 列名称
     */
    @Schema(description = "列名称")
    private String name;

    /**
     * 列描述
     */
    @Schema(description = "列描述")
    private String comment;

    /**
     * 列类型
     */
    @Schema(description = "列类型")
    private String columnType;

    /**
     * JAVA类型
     */
    @Schema(description = "JAVA类型")
    private String javaType;

    /**
     * JAVA字段名
     */
    @Schema(description = "JAVA字段名")
    private String javaField;

    /**
     * 大写JAVA字段名
     */
    @Schema(description = "大写JAVA字段名")
    private String bigJavaField;

    /**
     * 是否主键
     */
    @Schema(description = "是否主键")
    private Boolean primary;

    /**
     * 是否自增
     */
    @Schema(description = "是否自增")
    private Boolean increment;

    /**
     * 是否必填
     */
    @Schema(description = "是否必填")
    private Boolean required;

    /**
     * 是否唯一
     */
    @Schema(description = "是否唯一")
    private Boolean unique;

    /**
     * 是否为插入字段
     */
    @Schema(description = "是否为插入字段")
    private Boolean insert;

    /**
     * 是否为编辑字段
     */
    @Schema(description = "是否为编辑字段")
    private Boolean edit;

    /**
     * 是否为列表字段
     */
    @Schema(description = "是否为列表字段")
    private Boolean list;

    /**
     * 是否为查询字段
     */
    @Schema(description = "是否为查询字段")
    private Boolean query;

    /**
     * 查询方式
     */
    @Schema(description = "查询方式")
    private String queryType;

    /**
     * 显示类型
     */
    @Schema(description = "显示类型")
    private String htmlType;

    /**
     * 布局类型
     */
    @Schema(description = "布局类型")
    private String layoutType;

    /**
     * 字典类型标识
     */
    @Schema(description = "字典类型标识")
    private String dictCode;

    /**
     * 顺序
     */
    @Schema(description = "顺序")
    private BigDecimal sort;

    /**
     * 备注
     */
    @Schema(description = "备注")
    private String remark;

    public boolean isDeletedColumn() {
        return isDeletedColumn(this.javaField);
    }

    public boolean isDeletedColumn(String javaField) {
        return GeneratorConstants.DELETED_COLUMN.equalsIgnoreCase(javaField);
    }

    public boolean noNeedColumn() {
        return noNeedColumn(this.javaField);
    }

    public boolean noNeedColumn(String javaField) {
        return StringUtils.equalsAnyIgnoreCase(javaField, ArrayUtil.addAll(GeneratorConstants.COLUMN_NO_NEED));
    }

    public boolean isSuperColumn() {
        return isSuperColumn(this.javaField);
    }

    public static boolean isSuperColumn(String javaField) {
        return StringUtils.equalsAnyIgnoreCase(javaField, ArrayUtil.addAll(GeneratorConstants.BASE_ENTITY, GeneratorConstants.TREE_ENTITY));
    }

    public boolean isSpecialCharacters() {
        return isSpecialCharacters(this.javaField);
    }

    public static boolean isSpecialCharacters(String javaField) {
        return StringUtils.equalsAnyIgnoreCase(javaField, GeneratorConstants.SPECIAL_CHARACTERS);
    }

    public boolean isUsableColumn() {
        return isUsableColumn(javaField);
    }

    public static boolean isUsableColumn(String javaField) {
        // isSuperColumn() 中的名单用于避免生成多余 Domain 属性，若某些属性在生成页面时需要用到不能忽略，则放在此处白名单
        return StringUtils.equalsAnyIgnoreCase(javaField, "parentId", "orderNum", "remark");
    }

}